package com.bungarus;

import com.bungarus.busi.BusiRemedy;
import com.bungarus.busi.BusiServerChildHandlerInitializer;
import com.bungarus.busi.StatusContainerEhcache2;
import com.bungarus.cluster.NodeInfo;
import com.bungarus.model.AbstractBungarusServer;
import com.bungarus.model.AbstractBungarusServerWithMultipleClients;
import io.netty.channel.ChannelHandler;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by tang on 2018/11/25.
 */
public class Busi extends AbstractBungarusServerWithMultipleClients {
    public static final String TAG = "busi";
    /**
     * communication port with nodes in transfer cluster
     */
    private static final int PORT_7900 = 7900;
    /**
     * communication port with nodes in access cluster
     */
    private static final int PORT_7850 = 7850;
    /**
     * communication port with nodes in busi bridge cluster
     * provide service to deal with large group (members are greater than LARGE_GROUP_SIZE)
     */
    private static final int PORT_8000 = 8000;
    /**
     * the cluster name array to which busi node will connect as client
     */
    private static final String[] CLUSTER_TAGS = {CLUSTER_TAG_ACCESS, CLUSTER_TAG_BUSI_BRIDGE};
    /**
     * the capacity of largest group
     */
    public static final long LARGE_GROUP_SIZE = 500;

    public static void main(String[] args) throws Exception {
        int[] messagePorts = {PORT_7900, PORT_7850, PORT_8000};

        Busi busi = new Busi();
        busi.bind(System.getProperty(AbstractBungarusServer.HOST), messagePorts, null); // use messageport for heartbeat checking directly
        busi.initClusterAndConnectRemoteNode();
        busi.initRemedy();
        busi.listen(value -> {
            StatusContainerEhcache2.close();
            return null;
        });
    }

    @Override
    public void initRemedy() throws Exception {
        BusiRemedy.start(this);
    }

    @Override
    public ChannelHandler getChildHandlerInitializer() {
        return new BusiServerChildHandlerInitializer(this);
    }

    @Override
    public String[] getRemoteNodeTags() {
        return CLUSTER_TAGS;
    }

    @Override
    public CopyOnWriteArrayList<NodeInfo> calcRemoteServerNodes(CopyOnWriteArrayList<NodeInfo> originList) {
        return originList;
    }

    @Override
    public String getTag() {
        return TAG;
    }

    @Override
    public String tagPortString4Connection() {
        return "(access|7850,transfer|7900,busi-bridge|8000)";
    }
}
